package com.gy.common.util;//
//  Created by  fred on 2017/1/12.
//  Copyright © 2016年 Alibaba. All rights reserved.
//

import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.constant.SdkConstant;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.enums.ParamPosition;
import com.alibaba.cloudapi.sdk.enums.Scheme;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Set;

@Data
@Slf4j
public class ZtHttpApiClient extends ApacheHttpClient {

    private String path;

    public ZtHttpApiClient(ApiEnum apiEnum) {
        HttpClientBuilderParams httpClientBuilderParams = new HttpClientBuilderParams();
        httpClientBuilderParams.setScheme(Scheme.HTTP);
        httpClientBuilderParams.setHost(apiEnum.getHost());
        String appKey = apiEnum.getAppKey();
        httpClientBuilderParams.setAppKey(appKey);
        String appSecret = apiEnum.getAppSecret();
        httpClientBuilderParams.setAppSecret(appSecret);
        httpClientBuilderParams.setReadTimeout(60000L);
        httpClientBuilderParams.setWriteTimeout(60000L);
        httpClientBuilderParams.setConnectionTimeout(60000L);
        this.path = apiEnum.getApi();
        super.init(httpClientBuilderParams);
    }


    private static String getResultString(ApiResponse response) {
        StringBuilder result = new StringBuilder();
        result.append("Response from backend server").append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF);
        result.append("ResultCode:").append(SdkConstant.CLOUDAPI_LF).append(response.getCode()).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF);
        if (response.getCode() != 200) {
            result.append("Error description:").append(response.getHeaders().get("X-Ca-Error-Message")).append(SdkConstant.CLOUDAPI_LF).append(SdkConstant.CLOUDAPI_LF);
        }

        result.append("ResultBody:").append(SdkConstant.CLOUDAPI_LF).append(new String(response.getBody(), SdkConstant.CLOUDAPI_ENCODING));

        return new String(response.getBody(), SdkConstant.CLOUDAPI_ENCODING);
    }

    public String getBody(Integer pageNum, Integer pageSize, Map<String, Object> params) {
        ApiRequest request = new ApiRequest(HttpMethod.GET, this.path);
        request.addParam("pageNum", String.valueOf(pageNum), ParamPosition.QUERY, true);
        request.addParam("pageSize", String.valueOf(pageSize), ParamPosition.QUERY, true);
        Set<String> keySet = params.keySet();
        for (String key : keySet) {
            request.addParam(key, params.get(key).toString(), ParamPosition.QUERY, false);
        }
        ApiResponse response = this.sendSyncRequest(request);
        log.error("输出获取code {} --- {}", response.getCode(), request.getUrl() + request.getPath());
        try {
            return getResultString(response);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
